﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utils;
using MySql.Data.MySqlClient;

namespace Data
{
    public class Carreras
    {
        private Entities.Carrera CreateInstance(DataRow row)
        {
            return new Entities.Carrera(
                row["id"].ToInt32(),
                row["nombre"].ToString()
            );
        }

        private Entities.Collections.CarrerasCollection CreateCollection(DataTable table)
        {
            Entities.Collections.CarrerasCollection c = new Entities.Collections.CarrerasCollection();
            foreach (DataRow r in table.Rows) c.Add(CreateInstance(r));

            return c;
        }

        public Entities.Carrera GetByID(int id)
        {
            string s = "SELECT id, nombre FROM carreras WHERE id = @id";
            List<MySqlParameter> l = new List<MySqlParameter>();
            l.Add(new MySqlParameter("@id", id));
            Query q = new Query(s, l);
            DataTable t = q.Execute();

            if (t.Rows.Count > 0) return CreateInstance(t.Rows[0]);
            else return null;
        }

        public Entities.Collections.CarrerasCollection GetCollection()
        {
            string s = "SELECT id, nombre FROM carreras ORDER BY nombre";

            return CreateCollection(new Query(s, new List<MySqlParameter>()).Execute());
        }

        private void Insert(Entities.Carrera instance)
        {
            if (instance.State != Commons.EntityState.New) return;

            string s = "INSERT INTO carreras ( nombre ) VALUES ( @nombre )";
            List<MySqlParameter> l = new List<MySqlParameter>();
            l.Add(new MySqlParameter("@nombre", instance.Nombre));
            new NonQuery(s, l).Execute();
        }

        private void Update(Entities.Carrera instance)
        {
            if (instance.State != Commons.EntityState.Modified) return;

            string s = "UPDATE carreras SET nombre = @nombre WHERE id = @id";
            List<MySqlParameter> l = new List<MySqlParameter>();
            l.Add(new MySqlParameter("@nombre", instance.Nombre));
            l.Add(new MySqlParameter("@id", instance.ID));
            new NonQuery(s, l).Execute();
        }

        public void Delete(Entities.Carrera instance)
        {
            if (instance.State != Commons.EntityState.Deleted) return;

            string s = "DELETE FROM carreras WHERE id = @id";
            List<MySqlParameter> l = new List<MySqlParameter>();
            l.Add(new MySqlParameter("@id", instance.ID));
            new NonQuery(s, l).Execute();

            OnDelete(instance);
        }

        public void OnDelete(Entities.Carrera instance)
        {
            if (instance.State != Commons.EntityState.Deleted) return;

            // planes
            Planes planes = new Planes();
            planes.GetByCarrera(instance).ForEach(i =>
            {
                i.State = Commons.EntityState.Deleted;
                planes.Delete(i);
            });
        }

        public void Save(Entities.Carrera instance)
        {
            if (instance.State == Commons.EntityState.New) Insert(instance);
            else Update(instance);
        }
    }
}
